package com.haier.nengyuan.myapplication;

import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by wy on 2017/8/28.
 */

public class YearEnergyFragment extends BaseFragment {
    private ImageView mImgvEarning;
    private FrameLayout frameLayout;
    private TextView mTvYearEarningTotal;
    private float mYearToatal;
    private ArrayList<PointF> mPointArrayList = new ArrayList<>();
    private String mStrMonth;
    private String mStrDay;
    private String mStrHour;
    private float currDayEnergy;
    private float currHourEnergy;
    private float monTal;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_year_energy, container, false);
        frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);
        mTvYearEarningTotal = (TextView) view.findViewById(R.id.tv_earning_total);
        return view;
    }

    //    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @Override
//    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
////        WindowManager wm = getActivity().getWindowManager();
////        int width = (wm.getDefaultDisplay().getWidth() - 140) / 7;
////        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
////        Calendar rightNow = Calendar.getInstance();
////        for (int i = 0; i < Variable.nineMonthEnergy.length; i++) {
////            if (i < Integer.valueOf(mStrMonth)) {
////                mYearToatal += Variable.nineMonthEnergy[i];
////
////            }
////        }
//        Log.e("---", "onViewCreated: " + MyDateUtil.changeHM(System.currentTimeMillis()));
//        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
//        mStrDay = MyDateUtil.changeD(System.currentTimeMillis());
//        mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
//        Log.e("---", "onViewCreated: " + mStrMonth);
//        int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
//        if (mStrMonth.equals("09")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97));
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.nineDayEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);
//
//                    }
////                    mPointArrayList.add(new PointF(points4[i][0], currDayEnergy + currHourEnergy));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
////                    mPointArrayList.add(new PointF(points4[i][0], Variable.nineMonthEnergy[i]));
//
//                }
//            }
//            mPointArrayList.add(new PointF(points2[1][0], currHourEnergy + currDayEnergy + monTal));
//            float y = mPointArrayList.get(0).y;
//
//            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, (int) (y / 5.0));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * 1 + 60 + 145);
//            view1.setLayoutParams(layoutParams);
//            view1.setBackgroundColor(0xffffffff);
//
//            frameLayout.addView(view1);
//
//
//        }
//        if (mStrMonth.equals("10")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.tenDayEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.tenDayEnergy[i]) * (points4[i][1] / 31.97));
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.tenDayEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.tenDayEnergy[k]);
//
//                    }
////                    mPointArrayList.add(new PointF(points4[i][0], currDayEnergy + currHourEnergy));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
////                    mPointArrayList.add(new PointF(points4[i][0], Variable.nineMonthEnergy[i]));
//
//                }
//            }
//            mPointArrayList.add(new PointF(points2[1][0], currHourEnergy + currDayEnergy + monTal));
//            float y = mPointArrayList.get(0).y;
//
//            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, (int) (y / 5.0));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * 1 + 60 + 145);
//            view1.setLayoutParams(layoutParams);
//            view1.setBackgroundColor(0xffffffff);
//
//            frameLayout.addView(view1);
//
//
//        }
//        if (mStrMonth.equals("11")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.eleEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.eleEnergy[i]) * (points4[i][1] / 31.97));
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.eleEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.eleEnergy[k]);
//
//                    }
////                    mPointArrayList.add(new PointF(points4[i][0], currDayEnergy + currHourEnergy));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
////                    mPointArrayList.add(new PointF(points4[i][0], Variable.nineMonthEnergy[i]));
//
//                }
//            }
//            mPointArrayList.add(new PointF(points2[1][0], currHourEnergy + currDayEnergy + monTal));
//            float y = mPointArrayList.get(0).y;
//
//            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, (int) (y / 5.0));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * 1 + 60 + 145);
//            view1.setLayoutParams(layoutParams);
//            view1.setBackgroundColor(0xffffffff);
//
//            frameLayout.addView(view1);
//
//
//        }
//        mTvYearEarningTotal.setText((float) (Math.round((currHourEnergy + currDayEnergy + monTal) * 100)) / 100 + "");
//
//    }
    private float mTotal;
    private float[] dayEnergy;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onResume() {
        super.onResume();
        mTotal = 0;
        frameLayout.removeAllViews();

        WindowManager wm = getActivity().getWindowManager();
//        int width = (wm.getDefaultDisplay().getWidth() - 160) / 5;
        int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
        long time = System.currentTimeMillis();
        int mIntM = Integer.parseInt(MyDateUtil.changeM(time));
        int mIntD = Integer.parseInt(MyDateUtil.changeD(time));
        int hour = Integer.valueOf(MyDateUtil.changeH(time)) / 2;

        if (mIntM == 9) {
            dayEnergy = VirtualData.nineDayEnergy;
        } else if (mIntM == 10) {
            dayEnergy = VirtualData.tenDayEnergy;
        } else if (mIntM == 11) {
            dayEnergy = VirtualData.eleEnergy;
        } else if (mIntM == 12) {
            dayEnergy = VirtualData.decemberEnergy;
        } else if (mIntM == 1) {
            mIntM = 13;
            dayEnergy = VirtualData.oneEnergy;
        }


        float mD = 0;
        for (int i = 0; i < mIntM - 1; i++) {
            mD += VirtualData.pointsMouth[i];
        }
        for (int i = 0; i < mIntD - 1; i++) {
            mD += dayEnergy[i];
        }
        for (int i = 0; i <= hour; i++) {
            mD += (dayEnergy[mIntD - 1] * (VirtualData.pointsDay[i][1] / VirtualData.mdataDay));
        }
        mTotal = mD;

        for (int i = 0; i < 5; i++) {
            float y = 0;
            if (i == 1) {
                y = mD > 6667.52f ? 6667.52f : mD;

            }
            if (i == 2) {
                y = mD > 6667.52f ? mD - 6667.52f : 0;

            }


            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 40, (int) (y * 0.2));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * i + 60 + 20);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, (int) (y / 5.0));
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.setMarginStart((width) * 1 + 60 + 145);
            view1.setLayoutParams(layoutParams);
            view1.setBackgroundColor(0xffffffff);

            frameLayout.addView(view1);
        }
        mTvYearEarningTotal.setText((Math.round(mTotal * 100f)) / 100f + "");
    }

    @Override
    public int setRootView() {
        return R.layout.fragment_year;
    }

    @Override
    public void initViews() {
        mImgvEarning = (ImageView) mRootView.findViewById(R.id.imgv_earning);

    }

}
